package cn.yunyichina.reservation.interceptor;

import cn.yunyichina.reservation.common.ResponseEnum;
import cn.yunyichina.reservation.common.jackson.Jackson;
import cn.yunyichina.reservation.constant.Console;
import cn.yunyichina.reservation.constant.Encoding;
import cn.yunyichina.reservation.constant.HttpResponse;
import cn.yunyichina.reservation.constant.Redis;
import cn.yunyichina.reservation.util.HttpUtil;
import cn.yunyichina.reservation.util.ResponseUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * Created by Lullaby on 2016/8/23.
 */
public class ConsoleSessionInterceptor implements HandlerInterceptor {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object object) throws Exception {
        Cookie cookie = HttpUtil.getCookie(request, Console.CONSOLE_COOKIE_NAME);
        if (cookie != null) {
            String sidCookie = cookie.getValue();
            String username = new String(Base64.decodeBase64(sidCookie), Encoding.UTF8);
            String cacheKey = Redis.CONSOLE_SESSION_PREFIX.concat(username);
            String cacheValue = stringRedisTemplate.opsForValue().get(cacheKey);
            if (StringUtils.isBlank(cacheValue)) {
                ResponseUtil.response(Jackson.getBaseJsonData(
                        new HttpResponse().failure(
                                ResponseEnum.SESSION_TIMEOUT.getCode(),
                                ResponseEnum.SESSION_TIMEOUT.getMessage())),
                        response);
                return false;
            }
            request.setAttribute(Console.USERNAME, username);
            stringRedisTemplate.expire(cacheKey, Redis.CONSOLE_SESSION_TIMEOUT, TimeUnit.SECONDS);
            return true;
        } else {
            ResponseUtil.response(Jackson.getBaseJsonData(
                    new HttpResponse().failure(
                            ResponseEnum.SESSION_TIMEOUT.getCode(),
                            ResponseEnum.SESSION_TIMEOUT.getMessage())),
                    response);
            return false;
        }
    }

    public void postHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object object, ModelAndView view)
            throws Exception {

    }

    public void afterCompletion(
            HttpServletRequest request,
            HttpServletResponse response,
            Object object, Exception exception)
            throws Exception {

    }

}
